package com.nj9you.wxpay.dao;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;

import com.nj9you.wxpay.mapper.IOrderMapper;
import com.nj9you.wxpay.model.Order;

public class Dao {

    private static Logger logger = Logger.getLogger(Dao.class);
    private static final SqlSessionFactory sSqlSessionFactory;
    static {
        SqlSessionFactory tmp = null;
        try {
            tmp = new SqlSessionFactoryBuilder().build(Resources
                    .getResourceAsStream("/Configuration.xml"));
        } catch (IOException e) {
            e.printStackTrace();
            tmp = null;
        }
        sSqlSessionFactory = tmp;
        System.out.println("sSqlSessionFactory : " + sSqlSessionFactory);
    }

    public static List<Order> map() {
        return getAllOrder();
    }

    public static void init() {
    }

    private static SqlSession openSqlSession() {
        if (sSqlSessionFactory != null) {
            return sSqlSessionFactory.openSession();
        }
        return null;
    }

    public static void saveOrder(String merOrderId, String platformOrderId, String remark) {

        SqlSession sqlSession = null;
        try {
            Order order = new Order();
            order.setMerOrderId(merOrderId);
            order.setPlatformOrderId(platformOrderId);
            order.setRemark(remark);
            sqlSession = openSqlSession();
            IOrderMapper orderMapper = sqlSession.getMapper(IOrderMapper.class);
            orderMapper.saveOrder(order);
            sqlSession.commit(true);
        } catch (Exception e) {
            sqlSession.rollback(true);
            logger.error("error : " + e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    public static Order getOrder(String merOrderId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = openSqlSession();
            IOrderMapper orderMapper = sqlSession.getMapper(IOrderMapper.class);
            Order order = new Order();
            order.setMerOrderId(merOrderId);
            Order resultOrder = orderMapper.getOrder(order);
            return resultOrder;
        } catch (Exception e) {
            logger.error("error : " + e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return null;
    }

    public static List<Order> getAllOrder() {
        SqlSession sqlSession = null;
        try {
            sqlSession = openSqlSession();
            IOrderMapper orderMapper = sqlSession.getMapper(IOrderMapper.class);
            return orderMapper.getAllOrder();
        } catch (Exception e) {
            logger.error("error : " + e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return null;
    }
}
